<?php
namespace Mentors\Controller;
use Common\Controller\BaseController;

class MainController extends BaseController {

	public $userID;
    public $userName;
	public $userType;
    public $baseInfo=false;

    public function _initialize(){
    	parent::_initialize();

    	$this->userStatus = $this->checkUserStatus();

        if (!$this->userStatus) {
            $this->redirect(U("Home/Login/index"));
        }

        $this->userID = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userID'),C('cookie_fix'));
        $this->userName = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userName'),C('cookie_fix'));
        $this->userType = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userType'),C('cookie_fix'));

        $this->assign('userID',$this->userID);
        $this->assign('userName',$this->userName);
        $this->assign('userType',$this->userType);

        $user = M('Mentors')->where('userid='.$this->userID)->find();
        if ($user) {
            $this->baseInfo = true;
            $user['trainItem'] = trainStrtoArr($user['trainItem']);
            $this->assign('user',$user);
        }

        // $map['isread']=0;
        // $map['toID']=$this->userID;
        // $msgNum = M('Message')->where($map)->count();
        // $this->assign('msgNum',$msgNum);	

        M("Mentors")->where("userid=".$this->userID."")->setField("last_action_time",time());
    }

    public function checkUserStatus(){
    	$userID = cookie('userID');
        $userName = cookie('userName');
    	$userType = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userType'),C('cookie_fix'));
    	if(empty ($userName) || empty ($userID)|| $userType!=2){ 
    		return false;
        }else{
        	return true;
        }
    }


    //发送系统消息
    public function sendSysMsg($toID,$title,$content){
        $map['userid']=$toID;
        $userType=M("Member")->where($map)->getField("userType");
        if($userType){
            unset($map);
             $map['userid']=$toID;
            if($userType==1){
                $toUser=M("Mentees")->where($map)->find();
            }else{
                $toUser=M("Mentors")->where($map)->find();
            }
            $data['toID']=$toUser["userid"];
            $data['toName']=$toUser["nickname"];
            $data['toFace']=$toUser["face"];
            $data['fromID']=0;
            $data['fromName']='System';
            $data['fromFace']='';
            $data['type']=0;
            $data['isread']=0;
            $data['title']=$title;
            $data['content']=$content;
            $data['createTime']=time();
            M('Message')->add($data);
        }
    }
}